<template>
  <div class="index-container">
    <div class="header">
      <h1 class="title">企业信息查询</h1>
    </div>
    
    <!-- 业务介绍区域 -->
    <div class="business-section">
      <div class="business-banner">
        <div class="banner-content">
          <h2>专业企业信息查询服务</h2>
          <p>高效、准确、便捷的企业信息查询平台</p>
        </div>
      </div>
    </div>
    
    <div class="search-section">
      <div class="search-input-container">
        <input 
          type="text" 
          v-model="searchKeyword" 
          placeholder="请输入企业名称或统一社会信用代码" 
          class="search-input"
          @keyup.enter="searchCompany"
        />
        <button @click="searchCompany" class="search-button">查询</button>
      </div>
    </div>

    
    <!-- 底部按钮区域 -->
    <div class="bottom-buttons">
      <div class="bottom-button-item active" @click="navigateTo('/')">
        <span class="button-icon">🏠</span>
        <span class="button-text">首页</span>
      </div>
      <div class="bottom-button-item" @click="navigateTo('/pages/order/screenshot_order')">
        <span class="button-icon">📝</span>
        <span class="button-text">订单</span>
      </div>
      <div class="bottom-button-item" @click="navigateTo('/pages/user/index')">
        <span class="button-icon">👤</span>
        <span class="button-text">客服</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { searchCompanyList } from '@/api'

const router = useRouter()

// 响应式数据
const searchKeyword = ref('')
const isLoading = ref(false)



// 页面导航
const navigateTo = (path) => {
  router.push(path)
}

// 搜索企业信息
const searchCompany = async () => {
  const keyword = searchKeyword.value.trim()
  
  if (!keyword) {
    alert('请输入企业名称或统一社会信用代码')
    return
  }
  
  // 设置加载状态
  isLoading.value = true
  
  try {
    // 调用搜索接口
    const res = await searchCompanyList({
      keyword: keyword
    })
	if(res.code != 1)
	{
		alert(res.msg)
		return;
	}
    
    
    // 跳转到查询结果页，并传递搜索结果
    router.push({
      path: '/pages/result/index',
      query: {
        keyword: keyword,
        // 如果有数据也可以传递数据
        data: JSON.stringify(res.data || [])
      }
    })
    
  } catch (error) {
    console.error('搜索失败:', error)
  } finally {
    isLoading.value = false
  }
}

// 根据名称搜索
const searchCompanyByName = (name) => {
  searchKeyword.value = name
  searchCompany()
}


</script>

<style scoped>
/* 使用CSS变量保持与全局样式一致性 */
:root {
  --primary-color: #1989fa;
  --text-primary: #303133;
  --text-secondary: #606266;
  --bg-color: #f5f5f5;
  --white: #ffffff;
  --border-color: #e4e7ed;
}

.index-container {
  padding: 10px;
  width: 100%;
  min-height: 100vh;
  background-color: var(--bg-color);
  position: relative;
  padding-bottom: 80px; /* 为底部按钮留出空间 */
}

/* 头部区域 */
.header {
  text-align: center;
  padding: 30px 16px 20px;
  background-color: var(--primary-color);
  color: var(--white);
  position: relative;
}

.header::after {
  content: '已服务1000+企业';
  position: absolute;
  right: 16px;
  top: 10px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
}

.title {
  font-size: 20px;
  font-weight: 600;
  color: var(--white);
  margin: 0;
}

/* 搜索区域 */
.search-section {
  margin: 20px 16px;
  padding: 0;
}

.search-input-container {
  display: flex;
  position: relative;
  border-radius: 22px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.search-input {
  flex: 1;
  height: 44px;
  border: 1px solid var(--border-color);
  padding: 0 120px 0 20px;
  font-size: 14px;
  background-color: var(--white);
  outline: none;
}


.search-button {
  position: absolute;
  right: 1px;
  top: 1px;
  bottom: 1px;
  width: 90px;
  height: 42px;
  line-height: 42px;
  text-align: center;
  background-color: var(--primary-color);
  color: var(--white);
  font-size: 14px;
  border-radius: 0 21px 21px 0;
  border: none;
  cursor: pointer;
  transition: background-color 0.3s;
}

.search-button:hover {
  background-color: #0d7de0;
}

.search-button:disabled {
  background-color: #a0cfff;
  cursor: not-allowed;
}

/* 业务介绍区域 */
.business-section {
  padding: 0 16px 20px;
}

.business-banner {
  width: 100%;
  height: 150px;
  background: linear-gradient(135deg, #1890ff 0%, #40a9ff 100%);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
  margin-top: 10px; /* 向下移动10像素 */
  color: white;
  text-align: center;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.banner-content h2 {
  margin: 0 0 10px 0;
  font-size: 18px;
  font-weight: bold;
}

.banner-content p {
  margin: 0;
  font-size: 14px;
  opacity: 0.9;
}

/* 最近搜索区域 */
.recent-searches {
  margin: 0 16px 20px;
  background-color: var(--white);
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.recent-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.recent-header h3 {
  font-size: 14px;
  color: var(--text-primary);
  margin: 0;
}

.clear-btn {
  font-size: 12px;
  color: var(--text-secondary);
  cursor: pointer;
}

.clear-btn:hover {
  color: var(--primary-color);
}

.recent-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.recent-item {
  background-color: #f0f7ff;
  color: var(--primary-color);
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.3s;
}

.recent-item:hover {
  background-color: #e1f0ff;
  transform: translateY(-1px);
}

/* 底部按钮区域 */
.bottom-buttons {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background-color: var(--white);
  border-top: 1px solid var(--border-color);
  padding: 8px 0;
  z-index: 999;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
}

.bottom-button-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4px 0;
  cursor: pointer;
  transition: color 0.3s;
}

.bottom-button-item.active {
  color: var(--primary-color);
}

.bottom-button-item:hover {
  color: var(--primary-color);
}

.button-icon {
  font-size: 20px;
  margin-bottom: 4px;
}

.button-text {
  font-size: 12px;
  color: var(--text-secondary);
  transition: color 0.3s;
}

.bottom-button-item.active .button-text,
.bottom-button-item:hover .button-text {
  color: var(--primary-color);
}

/* 响应式设计 */
@media screen and (max-width: 320px) {
  .search-section {
    margin: 16px;
  }
  
  .business-banner {
    height: 100px;
  }
}

@media screen and (min-width: 768px) {
  .index-container {
    max-width: 480px;
    margin: 0 auto;
  }
  
  .bottom-buttons {
    max-width: 480px;
    left: 50%;
    transform: translateX(-50%);
  }
}
</style>